using System;

using Game.modules.map;
using GameFramework.buff;
using GameFramework.skill;

using UnityEngine;

public class PlayerCtl : MonoBehaviour
{
    
    public float speed = 5;
    
    private SkillManager skillManager;

    private MeshRenderer showMeshRender;
    // Start is called before the first frame update
    void Start()
    {
        skillManager = GetComponent<SkillManager>();
        showMeshRender = show.GetComponent<MeshRenderer>();
    }

    private float interval = 0.3f;

    private bool create = false;

    public GameObject show;
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            BuffConfig config = BuffConfigManager.Instance.getConfig(1);
            config.name = "测试改改";
        }

        if (Input.GetMouseButtonUp(0))
        {
            create = !create;
        }

        if (create)
        {
            show.SetActive(true);
            Ray screenPointToRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(screenPointToRay, out hit, LayerMask.GetMask("plane")))
            {
                Vector3 position = hit.point;
                Debug.Log("鼠标点击坐标："+position);
                Vector3 centerPos = MapManager.Instance.getCenterPos(position);
                Debug.Log("鼠标点的MapCell坐标："+centerPos+"  碰到的物体名："+hit.collider.name);
                if (Math.Abs(centerPos.x)>10 || Math.Abs(centerPos.y)>10)
                {
                    showMeshRender.material.color = Color.red;
                }
                else
                {
                    showMeshRender.material.color = Color.green;
                }

                show.transform.position = centerPos;
            }
        }
        else
        {
            show.SetActive(false);
        }


        float H = Input.GetAxis("Horizontal");
        float V = Input.GetAxis("Vertical");

        if (H!=0 || V!=0)
        {
            Vector3 direction = new Vector3(H, V).normalized;
            transform.Translate(direction*Time.deltaTime*speed);
        }
        
        if (Input.GetKeyUp(KeyCode.Space))
        {
            skillManager.releaseSkill(1);
        }
        interval -= Time.deltaTime;
        if (interval <= 0)
        {
            // skillManager.releaseSkill(1);
            interval = 0.3f;
        }
        
    }
}
